package com.system.permission.entity.DTO;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import com.system.permission.entity.Permission;
public class PermissionQueryDTO {
	private String permissionName;
	private String permissionType;
	private String url;
	public String getPermissionName() {
		return permissionName;
	}
	public void setPermissionName(String permissionName) {
		this.permissionName = permissionName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getPermissionType() {
		return permissionType;
	}
	public void setPermissionType(String permissionType) {
		this.permissionType = permissionType;
	}
	public static Specification<Permission> getSpecification(PermissionQueryDTO permissionQueryDTO){
		Specification<Permission> spec = new Specification<Permission>() {
			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<Predicate>();
				if(null !=permissionQueryDTO &&!StringUtils.isEmpty(permissionQueryDTO.getPermissionName())) {
					Predicate p1 = cb.like(root.get("permissionName").as(String.class), "%"+permissionQueryDTO.getPermissionName()+"%");
					list.add(p1);
				}
				if(null !=permissionQueryDTO &&!StringUtils.isEmpty(permissionQueryDTO.getUrl())) {
					Predicate p1 = cb.like(root.get("url").as(String.class), "%"+permissionQueryDTO.getUrl()+"%");
					list.add(p1);
				}
				if(null !=permissionQueryDTO &&!StringUtils.isEmpty(permissionQueryDTO.getPermissionType())) {
					Predicate p1 = cb.like(root.get("permissionType").as(String.class), "%"+permissionQueryDTO.getPermissionType()+"%");
					list.add(p1);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
